package com.ruoyi.gp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.MedicationRecordsVo;
import com.ruoyi.gp.domain.bo.MedicationRecordsBo;
import com.ruoyi.gp.service.IMedicationRecordsService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【用药纪录】
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/records")
public class MedicationRecordsController extends BaseController {

    private final IMedicationRecordsService iMedicationRecordsService;

    /**
     * 查询【用药纪录】列表
     */
    @SaCheckPermission("system:records:list")
    @GetMapping("/list")
    public TableDataInfo<MedicationRecordsVo> list(MedicationRecordsBo bo, PageQuery pageQuery) {
        return iMedicationRecordsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【用药纪录】列表
     */
    @SaCheckPermission("system:records:export")
    @Log(title = "【用药纪录】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MedicationRecordsBo bo, HttpServletResponse response) {
        List<MedicationRecordsVo> list = iMedicationRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "【用药纪录】", MedicationRecordsVo.class, response);
    }

    /**
     * 获取【用药纪录】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:records:query")
    @GetMapping("/{id}")
    public R<MedicationRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iMedicationRecordsService.queryById(id));
    }

    /**
     * 新增【用药纪录】
     */
    @SaCheckPermission("system:records:add")
    @Log(title = "【用药纪录】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add( @RequestPart MedicationRecordsBo bo, MultipartFile file) {
        return toAjax(iMedicationRecordsService.insertByBo(bo,file));
    }

    /**
     * 修改【用药纪录】
     */
    @SaCheckPermission("system:records:edit")
    @Log(title = "【用药纪录】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MedicationRecordsBo bo) {
        return toAjax(iMedicationRecordsService.updateByBo(bo));
    }

    /**
     * 删除【用药纪录】
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:records:remove")
    @Log(title = "【用药纪录】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMedicationRecordsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
